package uwosh.cs342.project1;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.LightingColorFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ContactInfoActivity extends Activity {
	// First paramenter - Context
	// Second parameter - Layout for the row
	// Third parameter - ID of the View to which the data is written
	// Forth - the Array of data
	private int contactID, groupID;
	private ArrayList<String> allGroups;
	private Intent contactUpdates;
	private int resultCode = 6005;
	private ListView listView;
	
	@Override
	public void onCreate (Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contactinfo_layout);
		Button editButton = (Button)findViewById(R.id.buttonEditContact);
		editButton.getBackground().setColorFilter(new LightingColorFilter(0xFFFF0000, 0xFFFF0000));
		
		//Get the User's Contact info that is sent in via a String Array
		Bundle extras = getIntent().getExtras(); 
		//contactInf[0] = FirstName
		//contactInf[1] = LastName
		//contactInf[2] = Address
		//contactInf[3] = CellPhone
		//contactInf[4] = HomePhone
		//contactInf[5] = 3rd Phone
		//contactInf[6] = Email
		//contactInf[7] = Comments
		groupID = extras.getInt("uwosh.cs342.project1.groupID");
		contactID = extras.getInt("uwosh.cs342.project1.childID");
		allGroups = extras.getStringArrayList("uwosh.cs342.project1.groups");
		String[] contactInf = extras.getStringArray("uwosh.cs342.project1.contactInfo");
		listView = (ListView) findViewById(R.id.listView1);
		String[] values = new String[] { contactInf[0]+ " " + contactInf[1], contactInf[2], "Cell #: " + contactInf[3],
				"Home# : " + contactInf[4], "Alt #: " + contactInf[5], "Email: " + contactInf[6], contactInf[7] };
		//String[] values = extras.getStringArray("uwosh.cs342.project1.contactInfo");
		//Populate ListView with array
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		android.R.layout.simple_list_item_1, android.R.id.text1, values);
		listView.setAdapter(adapter);
		
		contactUpdates = new Intent();
	}
	
	
	//Go back to ContactList from Contact Info via button
	public void homeButton(View view){
		setResult(resultCode, contactUpdates);
		finish();
	}
	
	public void editContact(View view){
		Intent myIntent = new Intent(view.getContext(), AddContactActivity.class);
		myIntent.putExtra("uwosh.cs342.project1.groups", allGroups);
		myIntent.putExtra("uwosh.cs342.project1.requestCode", 2000);
		startActivityForResult(myIntent, 2000);
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent intent){
		if (resultCode == 6001){
	    	Bundle extras = intent.getExtras();
	    	
	    	//pass updated contact information into the intent
	    	String firstName = extras.getString("uwosh.cs342.project1.firstName");
	    	String lastName = extras.getString("uwosh.cs342.project1.lastName");
	    	String email = extras.getString("uwosh.cs342.project1.email");
	    	String cellPhone = extras.getString("uwosh.cs342.project1.cellPhone");
	    	String homePhone = extras.getString("uwosh.cs342.project1.homePhone");
	    	String altPhone = extras.getString("uwosh.cs342.project1.altPhone");
	    	String comments = extras.getString("uwosh.cs342.project1.comments");
	    	String address = extras.getString("uwosh.cs342.project1.address");
	    	//int groupID = extras.getInt("uwosh.cs342.project1.groupId");
	    	
	    	contactUpdates.putExtra("uwosh.cs342.project1.firstName", firstName);
	    	contactUpdates.putExtra("uwosh.cs342.project1.lastName", lastName);
	    	contactUpdates.putExtra("uwosh.cs342.project1.email", email);
	    	contactUpdates.putExtra("uwosh.cs342.project1.cellPhone", cellPhone);
	    	contactUpdates.putExtra("uwosh.cs342.project1.homePhone", homePhone);
	    	contactUpdates.putExtra("uwosh.cs342.project1.altPhone", altPhone);
	    	contactUpdates.putExtra("uwosh.cs342.project1.comments", comments);
	    	contactUpdates.putExtra("uwosh.cs342.project1.address", address);
	    	contactUpdates.putExtra("uwosh.cs342.project1.groupId", groupID);
	    	contactUpdates.putExtra("uwosh.cs342.project1.childId", contactID);
	    	this.resultCode = 6003;
	    	
	    	String[] values = new String[] { firstName+ " " + lastName, address, "Cell #: " + cellPhone,
					"Home# : " + homePhone, "Alt #: " + altPhone, "Email: " + email, comments };
	    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	    			android.R.layout.simple_list_item_1, android.R.id.text1, values);
	    	listView.setAdapter(adapter);
    	}
    }//end onActivityResult
}
